package cn.edu.njnu.opengmpback.resource.controller;


import cn.edu.njnu.opengmpback.common.dto.PageDTO;
import cn.edu.njnu.opengmpback.common.utils.JsonResult;
import cn.edu.njnu.opengmpback.common.utils.ResultUtils;
import cn.edu.njnu.opengmpback.resource.entity.ModelItem;
import cn.edu.njnu.opengmpback.resource.entity.ResourceData;
import cn.edu.njnu.opengmpback.resource.entity.UserData;
import cn.edu.njnu.opengmpback.resource.entity.dto.*;
import cn.edu.njnu.opengmpback.resource.service.UserResService;
import cn.edu.njnu.opengmpback.user.entity.TokenInfo;
import cn.edu.njnu.opengmpback.user.entity.UserVo;
import cn.edu.njnu.opengmpback.user.service.UserService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping(value = "/userRes")
public class UserResController {

    @Autowired
    UserService userService;

    @Autowired
    UserResService userResService;

    @ApiOperation(value = "新建文件夹")
    @PostMapping("/createFolder")
    public JsonResult createFolder(@RequestBody CreateFolderDTO createFolderDTO, HttpServletRequest request) {
        UserVo userVo = (UserVo)userService.userState(request);
        String userId = userVo.getUserId();
        return userResService.createFolder(createFolderDTO, userId);
    }

    @ApiOperation(value = "批量上传数据")
    @PostMapping("/saveDataItem")
    public JsonResult saveDataItem(@RequestParam("file") MultipartFile [] multipartFile,
                                   @RequestParam("description") String description,
                                   @RequestParam("parentId") String parentId,
                                   @RequestParam("publicBoolean") boolean publicBoolean,
                                   @RequestParam("problemTags") String  problemTags,
                                   HttpServletRequest request) {
        DataItemDTO dataItemDTO = new DataItemDTO("", description,parentId,publicBoolean,"",problemTags);
        Object userState = userService.userState(request);
        if(userState.equals(false)){
            return ResultUtils.error(-1,"用户未登录");
        }
        UserVo userVo = (UserVo)userState;
        String userId = userVo.getUserId();
        return userResService.saveDataItem(multipartFile, dataItemDTO, userId);
    }

    @ApiOperation(value = "上传单个大文件")
    @PostMapping("/FileData")
    public JsonResult saveBigFileData(@RequestBody DataItemDTO dataItemDTO,
                                                  HttpServletRequest request){
        Object userState = userService.userState(request);
        if(userState.equals(false)){
            return ResultUtils.error(-1,"用户未登录");
        }
        UserVo userVo = (UserVo)userState;
        return userResService.saveBigFileData(dataItemDTO, userVo);
    }

    @ApiOperation(value = "用户发布数据")
    @PostMapping("/publishData")
    public JsonResult publishData(@RequestPart("info") CreateResourceDataDTO createResourceDataDTO,
                                  @RequestPart("files") List<Map<String, String>> files){
        return userResService.publishData(createResourceDataDTO, files);
    }

    @ApiOperation(value = "更新用户个人中心数据描述信息（不包括文件夹）")
    @PostMapping("/updateUserDataItem")
    public JsonResult updateUserDataItem(@RequestBody UpdateUserDataItemDTO updateUserDataItemDTO) {
        return userResService.updateUserDataItem(updateUserDataItemDTO);
    }

    @ApiOperation(value = "更新用户文件夹描述信息")
    @PostMapping("/updateUserFolder")
    public JsonResult updateUserFolder(@RequestBody UpdateFolderDTO updateFolderDTO) {
        return userResService.updateUserFolder(updateFolderDTO);
    }

    @ApiOperation(value = "获取用户某一层级的文件夹和文件数据")
    @PostMapping("/getUserResource")
    public JsonResult getUserResource(@RequestParam("parentId") String parentId, HttpServletRequest request) {
        Object userState = userService.userState(request);
        if(userState.equals(false)){
            return ResultUtils.error(-1,"用户未登录");
        }
        UserVo userVo = (UserVo)userState;
        String userId = userVo.getUserId();
        return userResService.getUserResource(parentId, userId);
    }

    @ApiOperation(value = "获取用户全部数据")
    @GetMapping(value = "/getUserAllResource")
    public JsonResult getUserInfo(HttpServletRequest request){
        Object userState = userService.userState(request);
        if(userState.equals(false)){
            return ResultUtils.error(-1,"用户未登录");
        }
        UserVo userVo = (UserVo)userState;
        String userId = userVo.getUserId();
        return userResService.getUserAllResource(userId);
    }



    @ApiOperation(value = "删除文件夹，或有数据级联删除")
    @PostMapping("/deleteFolder")
    public JsonResult deleteFolder(@RequestParam("folderId") String folderId) {
        return userResService.deleteFolder(folderId);
    }

    @ApiOperation(value = "删除数据")
    @PostMapping("/deleteDataItem")
    public JsonResult deleteDataItem(@RequestParam("dataItemId") String dataItemId) {
        return userResService.deleteDataItem(dataItemId);
    }


    @ApiOperation(value = "下载数据")
    @GetMapping("/downloadDataItem/{dataItemId}")
    public void downloadDataItem(@PathVariable("dataItemId") String dataItemId, HttpServletResponse response) {
        userResService.downloadDataItem(dataItemId, response);
    }


    @ApiOperation(value = "获取全部public的数据")
    @GetMapping(value = "/getAllPublicUserData")
    public JsonResult getAllPublicUserData(){
        return userResService.getAllPublicUserData();
    }

    @ApiOperation(value = "添加模型")
    @PostMapping(value = "/saveModel")
    public JsonResult saveModel(@RequestPart("info") CreateResourceModelDTO createResourceModelDTO, @RequestPart("imgFile") MultipartFile imgFile, HttpServletRequest request) {
        Object userState = userService.userState(request);
        if(userState.equals(false)){
            return ResultUtils.error(-1,"用户未登录");
        }
        UserVo userVo = (UserVo)userState;
        String userId = userVo.getUserId();
        return userResService.saveModel(createResourceModelDTO, imgFile, userId);
    }

    @ApiOperation(value = "添加方法")
    @PostMapping(value = "/saveMethod")
    public JsonResult saveMethod(@RequestPart("info") CreateResourceMethodDTO createResourceMethodDTO, @RequestPart("imgFile") MultipartFile imgFile, HttpServletRequest request) {
        Object userState = userService.userState(request);
        if(userState.equals(false)){
            return ResultUtils.error(-1,"用户未登录");
        }
        UserVo userVo = (UserVo)userState;
        String userId = userVo.getUserId();
        return userResService.saveMethod(createResourceMethodDTO, imgFile, userId);
    }

    @ApiOperation(value = "获取用户的所有模型和方法")
    @GetMapping("/getModelItemByUserId")
    public JsonResult getModelItemByUserId(HttpServletRequest request){
        Object userState = userService.userState(request);
        if(userState.equals(false)){
            return ResultUtils.error(-1,"用户未登录");
        }
        UserVo userVo = (UserVo)userState;
        String userId = userVo.getUserId();
        return userResService.getModelItemByUserId(userId);
    }

    @ApiOperation(value = "更新用户模型")
    @PostMapping(value = "/updateModel")
    public JsonResult updateModel(@RequestPart("info") UpdateResourceModelDTO updateResourceModelDTO, @RequestPart("imgFile") MultipartFile imgFile) {
        return userResService.updateModel(updateResourceModelDTO, imgFile);
    }

    @ApiOperation(value = "更新用户方法")
    @PostMapping(value = "/updateMethod")
    public JsonResult updateMethod(@RequestPart("info") UpdateResourceMethodDTO updateResourceMethodDTO, @RequestPart("imgFile") MultipartFile imgFile) {
        return userResService.updateMethod(updateResourceMethodDTO, imgFile);
    }

    @ApiOperation(value = "删除用户空间中模型/方法")
    @PostMapping("/removeModelItem")
    public JsonResult removeModelItem(@RequestParam("modelItemId") String modelItemId, HttpServletRequest request) {
        Object userState = userService.userState(request);
        if(userState.equals(false)){
            return ResultUtils.error(-1,"用户未登录");
        }
        UserVo userVo = (UserVo)userState;
        String userId = userVo.getUserId();
        return userResService.removeModelItem(modelItemId, userId);
    }

    @ApiOperation(value = "获取用户所有项目")
    @PostMapping("/getProjectsByUserId")
    public JsonResult getProjectsByUserId(HttpServletRequest request, @RequestBody PageDTO pageDTO) {
        Object userState = userService.userState(request);
        if(userState.equals(false)){
            return ResultUtils.error(-1,"用户未登录");
        }
        UserVo userVo = (UserVo)userState;
        String userId = userVo.getUserId();
        return userResService.getProjectsByUserId(userId, pageDTO);
    }

    @ApiOperation(value = "模型/方法添加到项目")
    @PostMapping("/modelItemToProject/{projectId}")
    public JsonResult modelItemToProject(@PathVariable("projectId") String projectId, @RequestBody List<String> modelItemList, HttpServletRequest request) {
        Object userState = userService.userState(request);
        if(userState.equals(false)){
            return ResultUtils.error(-1,"用户未登录");
        }
        UserVo userVo = (UserVo)userState;
        String userId = userVo.getUserId();
        return userResService.modelItemToProject(projectId, modelItemList, userId);
    }
}
